package com.retired_serviceman_management.service.impl;

import com.retired_serviceman_management.entity.Bulletin;
import com.retired_serviceman_management.dao.BulletinDao;
import com.retired_serviceman_management.service.BulletinService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Bulletin)表服务实现类
 *
 * @author makejava
 * @since 2023-03-23 18:00:30
 */
@Service("bulletinService")
public class BulletinServiceImpl implements BulletinService {
    @Resource
    private BulletinDao bulletinDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Bulletin queryById(Integer id) {
        return this.bulletinDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param bulletin 实例对象
     * @return 实例对象
     */
    @Override
    public Bulletin insert(Bulletin bulletin) {
        this.bulletinDao.insert(bulletin);
        return bulletin;
    }

    /**
     * 修改数据
     *
     * @param bulletin 实例对象
     * @return 实例对象
     */
    @Override
    public Bulletin update(Bulletin bulletin) {
        this.bulletinDao.update(bulletin);
        return this.queryById(bulletin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bulletinDao.deleteById(id) > 0;
    }

    @Override
    public List<Bulletin> view_bulletin() {
        List<Bulletin> bulletins = bulletinDao.queryByStatus();
        return bulletins;
    }

    @Override
    public List<Bulletin> queryAll() {
        List<Bulletin> bulletins = bulletinDao.queryAll();
        return bulletins;
    }

    @Override
    public Integer Update_Bulletin(Bulletin bulletin) {
        if(bulletin.getStatus()!=null&&bulletin.getStatus().equals("有效")) bulletin.setStatus("A");
        if(bulletin.getStatus()!=null&&bulletin.getStatus().equals("无效")) bulletin.setStatus("I");
        Bulletin bulletin1 = bulletinDao.queryById(bulletin.getId());
        if(bulletin.getTitle().equals(bulletin1.getTitle())&&bulletin.getContent().equals(bulletin1.getContent())&&bulletin.getStatus().equals(bulletin1.getStatus())) return 2;
        int i = bulletinDao.update(bulletin);
        return i;
    }

    @Override
    public Integer Insert_Bulletin(Bulletin bulletin) {
        Bulletin bulletin1 = bulletinDao.queryByTitle(bulletin.getTitle());
        if (bulletin1!=null) return 2;
        int i = bulletinDao.insert(bulletin);
        return i;
    }
}
